package edu.stanford.prpl.app.prpltalk.ui;

import static java.awt.Font.BOLD;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.stanford.prpl.api.BlobResource;
import edu.stanford.prpl.api.Identity;
import edu.stanford.prpl.api.PRPLAppClient;
import edu.stanford.prpl.app.prpltalk.PRPLTalk;
import edu.stanford.prpl.app.prpltalk.PRPLTalk.BuddyInfo;
import edu.stanford.prpl.app.prpltalk.ui.PrPlTalkGUI.ResourceListRenderer;

public class BuddyWindow extends JFrame {

	private static Log log = LogFactory.getLog(BuddyWindow.class);

	PRPLTalk prplTalk;
	PRPLAppClient prplAppClient;
	Identity identity;
	BuddyInfo buddyInfo;

	JList resourceList;

	public BuddyWindow(PRPLTalk prplTalk, BuddyInfo buddyInfo) {
		super(buddyInfo.name);
		this.prplTalk = prplTalk;
		this.prplAppClient = prplTalk.getPRPLAppClient();
		this.buddyInfo = buddyInfo;
		this.identity = buddyInfo.identity;

		this.setBounds(600, 0, 512, 512);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.initFrame();
	}

	public void start() {
		// this.pack();
		this.setVisible(true);
	}

	protected void initFrame() {

		JLabel buddyLabel = new JLabel("Buddy Info " + identity.getURI());
		buddyLabel.setFont(new Font("Arial", BOLD, 12));

		JTextArea displayTextArea = new JTextArea();
		displayTextArea.setRows(8);
		displayTextArea.setColumns(500);
		displayTextArea.setEditable(false);
		displayTextArea.setLineWrap(true);

		JPanel buttonPanel = initButtons();

		BlobResource[] resources = this.prplTalk.getAllCommunicationResourcesWith(this.identity.getURI());
		this.resourceList = new JList();
		this.resourceList.setListData(resources);
		this.resourceList.setCellRenderer(new ResourceListRenderer());

		JScrollPane resourceScrollPane = new JScrollPane();
		resourceScrollPane.getViewport().add(resourceList);

		this.resourceList.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == MouseEvent.BUTTON1) {
					BlobResource resource = (BlobResource) resourceList.getSelectedValue();
					log.debug("resourceList clicked (" + e.getClickCount() + "). Item: " + resource);
					if (e.getClickCount() == 2) {
						String sessionId = prplTalk.resourceURI2sessionId(resource.getURI());
						SessionWindow sessionWindow = new SessionWindow(prplTalk, sessionId);
						sessionWindow.start();
					}
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {

			}
		});

		this.getContentPane().setLayout(new BorderLayout(2, 2));
		this.getContentPane().add(buddyLabel, BorderLayout.PAGE_START);
		// this.getContentPane().add(displayScrollPane, BorderLayout.CENTER);
		this.getContentPane().add(resourceScrollPane, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
	}

	private JPanel initButtons() {
		JPanel buttonPanel = new JPanel();
		JButton createButton = new JButton("Create");
		JButton joinButton = new JButton("Join");
		JButton syncButton = new JButton("Sync");
		buttonPanel.add(createButton);
		buttonPanel.add(joinButton);
		buttonPanel.add(syncButton);

		createButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String sessionId = prplTalk.startSession(identity.getURI());
				BlobResource[] resources = prplTalk.getAllCommunicationResourcesWith(identity.getURI());
				resourceList.setListData(resources);
				SessionWindow sessionWindow = new SessionWindow(prplTalk, sessionId);
				sessionWindow.start();
			}
		});

		joinButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				BlobResource resource = (BlobResource) resourceList.getSelectedValue();
				String sessionId = prplTalk.resourceURI2sessionId(resource.getURI());
				SessionWindow sessionWindow = new SessionWindow(prplTalk, sessionId);
				sessionWindow.start();
			}
		});

		syncButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				prplTalk.synchWith(identity.getURI());
				BlobResource[] resources = prplTalk.getAllCommunicationResourcesWith(identity.getURI());
				resourceList.setListData(resources);
			}
		});

		return buttonPanel;
	}

}
